package com.example.controller;

import com.example.constant.Constants;
import com.example.model.*;
//import com.example.service.BookService;
import com.example.service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: bjc
 * Date: 2025-07-19
 * Time: 14:38
 */
@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {
    @Autowired
    private BookService bookService;


    /**
     * 添加图书
     */
    @PostMapping("/addBook")
    public String addBook(BookInfo bookInfo){
        //1.校验参数
        if(!StringUtils.hasLength(bookInfo.getBookName())
                ||!StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount() == null
                || bookInfo.getPrice() == null
                ||!StringUtils.hasLength(bookInfo.getPublish())
                || bookInfo.getStatus() == null){
            return "输入参数不合法";
        }
        try {
            //2.添加图书
            Integer result = bookService.insertBook(bookInfo);
            if(result > 0){
                //添加成功
                return "";
            }
        }catch (Exception e){
            System.out.println("出错");
        }
        return "添加失败";
    }

    /**
     * 查询图书列表，实现分页查询
     */
    @RequestMapping("/getBookListByPage")
    /*public PageResult<BookInfo> getBookListByPage(PageRequest pageRequest){
        log.info("获取图书列表，pageRequest:{}",pageRequest);
        return bookService.getBookListByPage(pageRequest);
    }*/
    //添加统一封装结果，修改相关代码
    public Result<PageResult<BookInfo>> getBookListByPage(PageRequest pageRequest,HttpSession session){
        log.info("获取图书列表，pageRequest:{}",pageRequest);
        /*//1.判断用户是否登录
        if(session.getAttribute(Constants.USER_SESSION_KEY) == null){
            return Result.noLogin();
        }
        //2.获取用户信息
        UserInfo loginUserInfo = (UserInfo) session.getAttribute(Constants.USER_SESSION_KEY);
        //3.判断用户信息是否存在
        if(loginUserInfo == null || loginUserInfo.getId() <0){
            return Result.fail("用户登陆信息出错");
        }
        //4.返回图书列表*/
        //因为配置了拦截器，所以不用上述代码了，而是直接配置到拦截器中
        PageResult<BookInfo> bookInfoPageResult = bookService.getBookListByPage(pageRequest);
        return Result.success(bookInfoPageResult);
    }

    /*
    实现修改图书接口
    1.点击修改按钮，获取原有图书信息
     */
    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer id){
        log.info("查询图书，id:{}",id);
        return bookService.queryBookById(id);
    }
    /*
    2.修改图书信息
     */
    @RequestMapping ("/updateBook")
    public String updateBook(BookInfo bookInfo){
        log.info("修改图书，bookInfo:{}",bookInfo);
        try {
            Integer result = bookService.updateBook(bookInfo);
            if(result > 0){
                //修改成功
                return "";
            }else {
                return "内部错误，请联系管理员";
            }
        }catch (Exception e){
            return "修改失败:"+e.getMessage();
        }
    }

    /**
     * 删除图书
     */
    @RequestMapping("/deleteBook")
    public String deleteBook(Integer id){
        log.info("删除图书，id:{}",id);
        try {
            Integer result = bookService.deleteBook(id);
            if(result > 0){
                //删除成功
                return "";
            }else {
                return "内部错误，请联系管理员";
            }
        }catch (Exception e){
            return "删除失败:"+e.getMessage();
        }
    }

    /**
     * 批量删除图书
     */
    @RequestMapping("/batchDeleteBook")
    public String batchDeleteBook(@RequestParam List<Integer> bookIds){
        log.info("批量删除图书，bookIds:{}",bookIds);
        try {
            Integer result = bookService.batchDeleteBook(bookIds);
            if(result > 0){
                return "";
            }else {
                return "内部错误,联系管理员";
            }
        } catch (Exception e) {
            return "删除图书失败"+e.getMessage();
        }
    }





}
